package com.lcj.mis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import com.lcj.common.entity.OrderStatusEnum;
import com.lcj.common.exception.AochException;
import com.lcj.common.util.PageUtils;
import com.lcj.common.util.Result;
import com.lcj.mis.controller.form.*;
import com.lcj.mis.feign.*;
import com.lcj.mis.service.OrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OdrServiceApi odrServiceApi;

    @Resource
    private CstServiceApi cstServiceApi;

    @Resource
    private DrServiceApi drServiceApi;

    @Resource
    private RuleServiceApi ruleServiceApi;

    @Resource
    private MpsServiceApi mpsServiceApi;

    @Resource
    private NebulaServiceApi nebulaServiceApi;

    @Override
    public PageUtils searchOrderByPage(SearchOrderByPageForm form) {
        Result r = odrServiceApi.searchOrderByPage(form);
        HashMap map = (HashMap) r.get("result");
        PageUtils pageUtils = BeanUtil.toBean(map, PageUtils.class);
        return pageUtils;
    }

    @Override
    public HashMap searchOrderComprehensiveInfo(long orderId) {
        HashMap map = new HashMap();

        //查询订单内容信息
        OrderIdForm form_1 = new OrderIdForm();
        form_1.setOrderId(orderId);
        Result r = odrServiceApi.searchOrderContent(form_1);
        if (!r.containsKey("result")) {
            throw new AochException("不存在订单记录");
        }
        HashMap content = (HashMap) r.get("result");
        map.put("content", content);

        //查询客户信息
        long customerId = MapUtil.getLong(content, "customerId");
        CustomerIdForm form_2 = new CustomerIdForm();
        form_2.setCustomerId(customerId);
        r = cstServiceApi.searchCustomerInfoInOrder(form_2);
        HashMap customerInfo = (HashMap) r.get("result");
        map.put("customerInfo", customerInfo);

        //查询司机信息
        long driverId = MapUtil.getLong(content, "driverId");
        DriverBaseInfoForm form_3 = new DriverBaseInfoForm();
        form_3.setDriverId(driverId);
        r = drServiceApi.searchDriverBriefInfo(form_3);
        HashMap driverInfo = (HashMap) r.get("result");
        map.put("driverInfo", Optional.ofNullable(driverInfo).orElse(new HashMap()));

        //查询消费规则
        if (content.containsKey("chargeRuleId")) {
            long chargeRuleId = MapUtil.getLong(content, "chargeRuleId");
            RuleIdForm form_4 = new RuleIdForm();
            form_4.setRuleId(chargeRuleId);
            r = ruleServiceApi.searchChargeRuleById(form_4);
            HashMap chargeRule = (HashMap) r.get("result");
            map.put("chargeRule", chargeRule);
        }

        //查询取消规则
        if (content.containsKey("cancelRuleId")) {
            long cancelRuleId = MapUtil.getLong(content, "cancelRuleId");
            RuleIdForm form_5 = new RuleIdForm();
            form_5.setRuleId(cancelRuleId);
            r = ruleServiceApi.searchCancelRuleById(form_5);
            HashMap cancelRule = (HashMap) r.get("result");
            map.put("cancelRule", cancelRule);
        }

        //查询分账规则
        if (content.containsKey("profitsharingRuleId")) {
            long profitsharingRuleId = MapUtil.getLong(content, "profitsharingRuleId");
            RuleIdForm form_6 = new RuleIdForm();
            form_6.setRuleId(profitsharingRuleId);
            r = ruleServiceApi.searchProfitsharingRuleById(form_6);
            HashMap profitsharingRule = (HashMap) r.get("result");
            map.put("profitsharingRule", profitsharingRule);
        }


        //查询GPS规划线路
        CalculateDriveLineForm form_7 = new CalculateDriveLineForm();
        HashMap startPlaceLocation = (HashMap) content.get("startPlaceLocation");
        HashMap endPlaceLocation = (HashMap) content.get("endPlaceLocation");
        //封装GPS表单
        form_7.setStartPlaceLatitude(MapUtil.getStr(startPlaceLocation, "latitude"));
        form_7.setStartPlaceLongitude(MapUtil.getStr(startPlaceLocation, "longitude"));
        form_7.setEndPlaceLatitude(MapUtil.getStr(endPlaceLocation, "latitude"));
        form_7.setEndPlaceLongitude(MapUtil.getStr(endPlaceLocation, "longitude"));

        r = mpsServiceApi.calculateDriveLine(form_7);
        HashMap driveLine = (HashMap) r.get("result");
        map.put("driveLine", driveLine);

        int status = MapUtil.getInt(content, "status");

        //1等待接单，2已接单，3司机已到达，4开始行程，5结束行程，6未付款，7已付款，8订单已结束，9顾客撤单，10司机撤单，11事故关闭，12其他
        if (status >= OrderStatusEnum.END_DRIVING.getCode().intValue() && status <= OrderStatusEnum.FINISH.getCode().intValue()) {
            //查询订单GPS定位记录
            r = nebulaServiceApi.searchOrderGps(form_1);
            ArrayList<HashMap> orderGps = (ArrayList<HashMap>) r.get("result");
            map.put("orderGps", orderGps);
        } else if (status == OrderStatusEnum.BEGIN_DRIVING.getCode().intValue()) {
            //查询订单中最后的GPS定位
            r = nebulaServiceApi.searchOrderLastGps(form_1);
            HashMap lastGps = (HashMap) r.get("result");
            map.put("lastGps", lastGps);
        }
        return map;
    }

    @Override
    public ArrayList<HashMap> searchOrderStartLocationIn30Days() {
        Result r = odrServiceApi.searchOrderStartLocationIn30Days();
        ArrayList<HashMap> list = (ArrayList<HashMap>) r.get("result");
        ArrayList<HashMap> result = new ArrayList<>();
        Map<HashMap, Integer> map = CollectionUtil.countMap(list);
        map.forEach((key, value) -> {
            key.replace("latitude", MapUtil.getDouble(key, "latitude"));
            key.replace("longitude", MapUtil.getDouble(key, "longitude"));
            //数据量太小看不到热力点
            key.put("count", value*1000);
            result.add(key);
        });
        return result;
    }

    @Override
    public Result searchReviewDriverOrderBill(OrderIdForm form) {
        return odrServiceApi.searchReviewDriverOrderBill(form);
    }

    @Override
    public Result updateOrderStatus(UpdateOrderStatusForm form) {
        return odrServiceApi.updateOrderStatus(form);
    }
}